// pages/center/center.js
var httpUtil = require('../../utils/httpUtil.js');
var util = require('../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    centerimg: 'https://swcj.majiangyun.com/img/ioc/user-bg.png',
    status: false,
    userInfo: {},
    userUrl: "https://swcj.majiangyun.com/img/userUrl.png"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userInfo = util.getKey("userInfo");
    if (userInfo.status == 0) {
      that.setData({
        status: true
      })
    }
    if (userInfo == false || userInfo == '') {
      app.onLaunch();
    }
    that.setData({
      userInfo: userInfo,
      userUrl: userInfo == '' ? "https://swcj.majiangyun.com/img/userUrl.png" : userInfo.userUrl
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  goMember: function () {
    wx.navigateTo({
      url: '../member/member'
    })
  },
  goCoupon: function () {
    wx.navigateTo({
      url: '../coupon/coupon'
    })
  },
  goCode: function () {
    wx.navigateTo({
      url: '../code/code'
    })
  },
  orderLook: function (e) {
    var currentTab = e.target.dataset.id;
    wx.navigateTo({
      url: '../orderList/orderList?currentTab=' + currentTab
    })
  },
  goLogistics: function () {
    wx.navigateTo({
      url: '../integral/integral'
    })
  },
  //获取信息
  onGotUserInfo: function (res) {
    var that = this;

    if (res.detail.errMsg == "getUserInfo:ok") {
      var userInfo = that.data.userInfo;
      if (userInfo == false || userInfo == '') {
        app.onLaunch();
      } else {

        var model = JSON.parse(res.detail.rawData);
        console.log(model);

        userInfo.userName = model.nickName;
        userInfo.userUrl = model.avatarUrl;
        userInfo.status = 1;
        httpUtil.postHttp("/user/setUser", userInfo, function (res) {
          that.setData({
            userInfo: res,
            userUrl: res.userUrl,
            status: false
          })

          wx.setStorage({
            key: "userInfo",
            data: res
          })

        })
      }

    }
    else {
      console.log("你拒绝了授权");
    }
  }, getAnswer:function(){
    wx.navigateTo({
      url: '../answer/answer?id=' + 66
    })
  }
})